//
//  NewPasswordViewController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/25.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "NewPasswordViewController.h"

@interface NewPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (nonatomic , strong) UIButton * timeBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;

@end

@implementation NewPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)setNavi
{
    self.title = @"设置新密码";
}

- (void)setUI
{
    self.view.backgroundColor =  Color(245, 245, 245);
    
    self.timeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.timeBtn.titleLabel.font = FontSize(12);
    [self.timeBtn setTitleColor:TextLessImportColor forState:(UIControlStateNormal)];
    self.timeBtn.frame = CGRectMake(CGRectGetWidth(self.codeText.frame) - 70, self.codeText.frame.size.height / 2 - 15, 50, 30);
    [self.timeBtn setTitle:@"60秒" forState:(UIControlStateNormal)];
    [self.codeText addSubview:self.timeBtn];
    self.timeBtn.userInteractionEnabled = NO;
    [self.timeBtn addTarget:self action:@selector(getCode) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self openCountdown];
    
    [self.codeText addTarget:self action:@selector(changeLength:) forControlEvents:(UIControlEventEditingChanged)];
    [self.passwordText addTarget:self action:@selector(changeLength:) forControlEvents:(UIControlEventEditingChanged)];
    
}

- (void)changeLength:(UITextField *)textField
{
    if (self.codeText.text.length == 6 && self.passwordText.text.length >= 6) {
        self.doneBtn.userInteractionEnabled = YES;
        [self.doneBtn setBackgroundColor:MainColor];
    }else
    {
        [self.doneBtn setBackgroundColor:MainBtnColor];
        self.doneBtn.userInteractionEnabled = NO;
    }
}

- (void)getCode
{
    [self openCountdown];
}

// 开启倒计时效果
-(void)openCountdown{
    
    [self.timeBtn startWithTime:59 title:@"获取验证码" countDownTitle:@"重新发送" mainColor:[UIColor clearColor] countColor:[UIColor clearColor]];
}

- (IBAction)doneAction:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"phone_redis_key":self.redisKey,@"new_password":self.passwordText.text,@"sms_code":self.codeText.text};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/find-login-password-sms",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            [MBProgressHUD showSuccess:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


- (IBAction)safeAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    _passwordText.secureTextEntry = !sender.selected;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
